from FiniteStateMachine import FiniteStateMachine
from pcaspy import SimpleServer, Driver
from config import get_config
from os import environ
from os.path import isfile

stop_thread = False


class myDriver(Driver):
    def __init__(self):
        super(myDriver, self).__init__()


# 程序入口
if __name__ == "__main__":
    if isfile("./config.py"):
        from config import get_config

    others_datas = get_config()["others"]
    pv_datas = get_config()["PV"]
    environ["EPICS_CA_ADDR_LIST"] = others_datas["EPICS_CA_ADDR_LIST"]
    environ["EPICS_CAS_SERVER_PORT"] = others_datas["EPICS_CAS_SERVER_PORT"]
    pvdb = {
        pv_datas["急停开关"]: {"type": "enum", "enums": ["STOP", "RUN"]},
        pv_datas["启动开关"]: {"type": "enum", "enums": ["STOP", "RUN"]},
        pv_datas["闭环至升功率开关"]: {
            "type": "enum",
            "enums": ["STOP", "RUN"],
        },
        pv_datas["降维持开关"]: {"type": "enum", "enums": ["STOP", "RUN"]},
        pv_datas["升维持开关"]: {"type": "enum", "enums": ["STOP", "RUN"]},
        pv_datas["开环功率设置"]: {"prec": "3"},
        pv_datas["闭环维持功率设置"]: {"prec": "3"},
        pv_datas["点火功率设置"]: {"prec": "3"},
        pv_datas["清洗功率设置"]: {"prec": "3"},
        pv_datas["降功率清洗功率"]: {"prec": "3"},
        pv_datas["升功率清洗功率"]: {"prec": "3"},
        pv_datas["点火时间"]: {"prec": "1"},
        pv_datas["点火维持时间"]: {"prec": "1"},
        pv_datas["清洗时间"]: {"prec": "1"},
        pv_datas["当前清洗时间"]: {"prec": "1"},
        pv_datas["降功率清洗时间"]: {"prec": "1"},
        pv_datas["升功率清洗时间"]: {"prec": "1"},
        pv_datas["降功率时间"]: {"prec": "1"},
        pv_datas["升功率时间"]: {"prec": "1"},
        pv_datas["清洗结束降功率时间"]: {"prec": "1"},
        pv_datas["清洗结束闭环维持时间"]: {"prec": "1"},
        pv_datas["激励检查点"]: {"prec": "3"},
        pv_datas["激励状态"]: {"type": "enum", "enums": ["UN_IGNITION", "IGNITION"]},
        pv_datas["S11_12"]: {"prec": "3"},
        pv_datas["S11_34"]: {"prec": "3"},
        pv_datas["位置判断电压"]: {"prec": "3"},
        pv_datas["清洗上部时间"]: {"prec": "3"},
        pv_datas["清洗下部时间"]: {"prec": ""},
        pv_datas["清洗上部次数"]: {"prec": "0"},
        pv_datas["清洗下部次数"]: {"prec": "0"},
        pv_datas["数据清零"]: {"type": "enum", "enums": ["UN_CLEAN", "CLEAN"]},
        # pv_datas["X_Axis_Wave"]: {"count": others_datas["数据大小"], "prec": "1"},
        # pv_datas["RF_IN1_Wave"]: {"count": others_datas["数据大小"], "prec": "5"},
        # pv_datas["RF_IN2_Wave"]: {"count": others_datas["数据大小"], "prec": "5"},
        # pv_datas["RF_IN3_Wave"]: {"count": others_datas["数据大小"], "prec": "5"},
        # pv_datas["RF_IN4_Wave"]: {"count": others_datas["数据大小"], "prec": "5"},
        pv_datas["存储数据开关"]: {
            "type": "enum",
            "enums": ["START", "STOP"],
        },
    }

    server = SimpleServer()
    server.createPV("", pvdb)
    driver = myDriver()
    fsm = FiniteStateMachine()
    try:
        while not stop_thread:
            # process CA transactions
            server.process(0.1)
    except KeyboardInterrupt:
        print("捕获到 Ctrl+C,正在关闭线程...")
        stop_thread = True
        fsm.stop_thread = True  # 设置标志以停止线程
